#include "cw32l010_rtc.h"
#include "cw32l010_sysctrl.h"
#include "bsp_rtc.h"

void bsp_rtc_init(void)
{
  RTC_DeInit();

  __SYSCTRL_RTC_CLK_ENABLE();

  RTC_TimeTypeDef RTC_TimeStruct = {0};
  RTC_GetTime(&RTC_TimeStruct);

  RTC_AlarmTypeDef RTC_AlarmStruct = {0};
  /*高位是置位代表启用， 时间是置位代表忽略，不忽略的代表是匹配值*/
  RTC_AlarmStruct.RTC_AlarmMask = RTC_AlarmMask_Hours | RTC_AlarmMask_Minutes | 0x7F000000;
  RTC_AlarmStruct.RTC_AlarmTime.H24 = 1;
  RTC_AlarmStruct.RTC_AlarmTime.Hour = 0x00;
  RTC_AlarmStruct.RTC_AlarmTime.Minute = 0x00;
  RTC_AlarmStruct.RTC_AlarmTime.Second = 0x01;
  RTC_SetAlarm(RTC_Alarm_A, &RTC_AlarmStruct);
  RTC_AlarmCmd(RTC_Alarm_A, DISABLE);

  RTC_InitTypeDef RTC_InitStruct = {0};

  RTC_InitStruct.DateStruct.Day = 1;
  RTC_InitStruct.DateStruct.Month = RTC_Month_January;
  RTC_InitStruct.DateStruct.Year = 25;
  RTC_InitStruct.DateStruct.Week = RTC_Weekday_Wednesday;
  RTC_InitStruct.RTC_ClockSource = RTC_RTCCLK_FROM_LSI;
  RTC_InitStruct.TimeStruct.H24 = RTC_HOUR24;
  RTC_InitStruct.TimeStruct.Hour = 0;
  RTC_InitStruct.TimeStruct.Minute = 0;
  RTC_InitStruct.TimeStruct.Second = 0;

  RTC_ClearITPendingBit(RTC_IT_ALL);
  // RTC_SetInterval(RTC_INTERVAL_EVERY_1S); // 1s间隔一次产生中断
  RTC_ITConfig(RTC_IT_AWTIMER, ENABLE); // | RTC_IT_INTERVAL |RTC_IT_ALARMA

  NVIC_SetPriority(RTC_IRQn, 1);
  NVIC_EnableIRQ(RTC_IRQn);

  RTC_Init(&RTC_InitStruct);
}

void RTC_IRQHandler(void)
{
  /* USER CODE BEGIN */
  if (RTC_GetITState(RTC_IT_ALARMA))
  {
    RTC_ClearITPendingBit(RTC_IT_ALARMA);
  }
  if (RTC_GetITState(RTC_IT_INTERVAL))
  {
    RTC_ClearITPendingBit(RTC_IT_INTERVAL);
  }
  if (RTC_GetITState(RTC_IT_AWTIMER))
  {
    CW_UART1->TDR = 0X36;
    RTC_ClearITPendingBit(RTC_IT_AWTIMER);
  }
  /* USER CODE END */
}

void bsp_setRTC_AlarmTime(int h, int m, int s)
{
  RTC_UNLOCK();
  uint32_t mask = 0x7F000000;

  if (h <= 0x24)
  {
    if (h == 0x24)
      h = 0;
  }
  else
  {
    mask |= RTC_AlarmMask_Hours;
  }

  if (m <= 0x60)
  {
    if (m == 0x60)
      m = 0;
  }
  else
  {
    mask |= RTC_AlarmMask_Minutes;
  }

  if (s <= 0x60)
  {
    if (s == 0x60)
      s = 0;
  }
  else
  {
    mask |= RTC_AlarmMask_Seconds;
  }

  RTC_AlarmCmd(RTC_Alarm_A, DISABLE);

  RTC_AlarmTypeDef RTC_AlarmStruct = {0};
  /*高位是置位代表启用， 时间是置位代表忽略，不忽略的代表是匹配值*/
  RTC_AlarmStruct.RTC_AlarmMask = mask;
  RTC_AlarmStruct.RTC_AlarmTime.H24 = 1;
  RTC_AlarmStruct.RTC_AlarmTime.Hour = h;
  RTC_AlarmStruct.RTC_AlarmTime.Minute = m;
  RTC_AlarmStruct.RTC_AlarmTime.Second = s;
  RTC_SetAlarm(RTC_Alarm_A, &RTC_AlarmStruct);

  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
  RTC_LOCK();
}

void bsp_setRTC_Close(void)
{
  __SYSCTRL_RTC_CLK_DISABLE();
  RTC_DeInit();

  RTC_ClearITPendingBit(RTC_IT_ALL);
  RTC_ITConfig(RTC_IT_ALL, DISABLE); // | RTC_IT_INTERVAL

  NVIC_SetPriority(RTC_IRQn, 1);
  NVIC_DisableIRQ(RTC_IRQn);
}

void bsp_setRTC_AutoWakeup(unsigned short s)
{
  // CW_RTC->CR2_f.AWTPRS = 0x00;
  RTC_AWT_PSC_set(0, 16000 - 1);
  RTC_AWTTypeDef RCT_AWTStruct;
  RCT_AWTStruct.AWT_ClockSource = RTC_AWTSOURCE_FROM_RTC1HZ_1;
  RCT_AWTStruct.AWT_ARRValue = s;
  RTC_AWTConfig(&RCT_AWTStruct);
  RTC_AWTCmd(ENABLE);
}
